package com.x.crm.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.x.crm.model.*;
import com.x.crm.rpc.*;
import com.x.erp.rpc.*;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * Created by Administrator on 2018/1/10.
 */


@RestController
@RequestMapping("/resForApp")
public class ResForAppController extends BaseController {

	@Reference
	private CustomerRpcService customerRpcService;
	@Reference
	private CustomerShopRpcService customerShopRpcService;
	@Reference
	private SeriesRpcService seriesRpcService;
	@Reference
	private ProvinceRpcService provinceRpcService;
	@Reference
	private CityRpcService cityRpcService;
	@Reference
	private IntentionRpcService intentionRpcService;
	@Reference
	private UserRpcService userRpcService;
	@Reference
	private FollowRpcService followRpcService;
	@Reference
	private IntentionModelRpcService intentionModelRpcService;
	@Reference
	private CarRpcService carRpcService;
	@Reference
	private ShopRpcService shopRpcService;
	@Reference
	private BrandRpcService brandRpcService;
	@Reference
	private PhoneRecordRpcService phoneRecordRpcService;


	/**
	 * 客戶意向列表APP
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/getCustomerModelList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCustomerModelList(IntentionModel intentionModel, CustomerShop customerShop) {
		if (StringUtils.isEmpty(customerShop.getCustomerShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客戶不能為空");
		}
		CustomerShop customerShopInfo = customerShopRpcService.selectCustomerShopList(customerShop).get(0);
		if (customerShopInfo == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "沒有此用戶");
		}
		Customer customer = customerShopInfo.getCustomer();
		List<IntentionModel> intentionModelList = intentionModelRpcService.selectModelList(intentionModel, customerShop);
		for (IntentionModel model : intentionModelList) {
			model.setIntention(null);
		}
		Map<String, Object> res = new HashMap<>();
		res.put("customerName", customer.getCustomerName());
		res.put("customerPhone", customer.getCustomerPhone());
		res.put("customerGender", customer.getCustomerGender());
		res.put("provinceName", customer.getProvinceName());
		res.put("cityName", customer.getCityName());
		res.put("followState", customerShopInfo.getFollowState());
		res.put("customerShopId", customerShopInfo.getCustomerShopId());
		res.put("customerBudgetMin", customerShopInfo.getCustomerBudgetMin());
		res.put("customerBudgetMax", customerShopInfo.getCustomerBudgetMax());
		res.put("remark", customerShopInfo.getCustomerRemark());
		res.put("channelName", customerShopInfo.getChannelName());
		res.put("levelTxt", customerShopInfo.getLevelTxt());
		res.put("modelList", intentionModelList);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}


	/**
	 * 查询客户详情
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/customerInfoList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCustomerInfoList(CustomerShop customerShop, Boolean isReach) {
		if (StringUtils.isEmpty(customerShop.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
		}
		if (isReach != null) {
			customerShop.setReach(isReach);
		}
		List<CustomerShop> customerShopList = customerShopRpcService.selectCustomerShopList(customerShop);
		List<Map<String, Object>> resultList = new ArrayList<>();
		Map<String, Object> res = null;
		for (CustomerShop model : customerShopList) {
			res = new HashMap<>();
			res.put("customerInfo", model);
			List<IntentionModel> modelList = intentionModelRpcService.selectModelList(new IntentionModel(), model);
			if (modelList.size() > 0) {
				res.put("brandName", modelList.get(0).getBrandName());
				res.put("seriesName", modelList.get(0).getSeriesName());
				res.put("carTitle", modelList.get(0).getCarTitle());
			}
			Follow follow = new Follow();
			follow.setCustomerShopId(model.getCustomerShopId());
			List<Follow> followList = followRpcService.selectFollowList(follow);
			if (followList.size() > 0) {
				res.put("lastFollowInfo", followList.get(0));
			}
			resultList.add(res);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", resultList);
	}

	/**
	 * 通讯录查询接口
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/phoneRecordList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel phoneRecordList(PhoneRecord phoneRecord) {
		if (StringUtils.isEmpty(phoneRecord.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "当前登录人ID不能为空");
		}
		List<PhoneRecord> phoneRecordList = phoneRecordRpcService.selectPhoneRecordList(phoneRecord);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", phoneRecordList);
	}


	/**
	 * 通讯录查询接口
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/phoneRecordNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel phoneRecordNum(PhoneRecord phoneRecord) {
		if (StringUtils.isEmpty(phoneRecord.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "当前登录人ID不能为空");
		}
		int res = phoneRecordRpcService.selectPhoneRecordNum(phoneRecord);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 通讯录插入接口
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/insertPhoneRecord", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertPhoneRecord(PhoneRecord phoneRecord) {
		if (StringUtils.isEmpty(phoneRecord.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户ID不能为空");
		}
		if (StringUtils.isEmpty(phoneRecord.getCustomerName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户名称不能为空");
		}
		if (StringUtils.isEmpty(phoneRecord.getCustomerPhone())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户电话不能为空");
		}
		List<PhoneRecord> resList = phoneRecordRpcService.selectPhoneRecordList(phoneRecord);
		phoneRecord.setRecordState(1);
		PhoneRecord phoneRecordInfo = null;
		if (resList != null && !resList.isEmpty()) {
			phoneRecord.setPhoneRecordId(resList.get(0).getPhoneRecordId());
			phoneRecordRpcService.updatePhoneRecord(phoneRecord);
			phoneRecordInfo = phoneRecord;
		} else {
			phoneRecordInfo = phoneRecordRpcService.insertPhoneRecord(phoneRecord);
		}

		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", phoneRecordInfo);
	}

	/**
	 * 通讯录查询接口
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/updatePhoneRecord", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updatePhoneRecord(PhoneRecord phoneRecord) {
		if (StringUtils.isEmpty(phoneRecord.getCustomerName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户名称不能为空");
		}
		if (StringUtils.isEmpty(phoneRecord.getCustomerPhone())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "客户电话不能为空");
		}
		if (StringUtils.isEmpty(phoneRecord.getPhoneRecordId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "通讯录ID不能为空");
		}
		phoneRecord.setRecordState(1); //更新后将无效变有效
		phoneRecordRpcService.updatePhoneRecord(phoneRecord);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "更新成功");
	}


	/**
	 * 删除通讯录接口
	 *
	 * @param
	 * @return
	 */
	
	@RequestMapping(value = "/deletePhoneRecord", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deletePhoneRecord(PhoneRecord phoneRecord) {
		if (StringUtils.isEmpty(phoneRecord.getPhoneRecordId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "通讯录ID不能为空");
		}
		phoneRecord.setRecordState(0);
		phoneRecordRpcService.updatePhoneRecord(phoneRecord);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功");
	}






	/**
	 * 工作台数量
	 *
	 * @param
	 * @return
	 */

	
	@RequestMapping(value = "/bashboard", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel bashboard(CustomerShop customerShop) {
		if (StringUtils.isEmpty(customerShop.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
		}
		Map<String, Integer> map = new HashMap<>();
		map.put("xkhdfp", 0);
		map.put("ygwcl", 0);
		map.put("xfpdcl", 0);
		map.put("jrwcl", 0);
		map.put("gqwcl", 0);
		CustomerShop customerShopInfo = new CustomerShop();
		customerShopInfo.setShopId(customerShop.getShopId());
		for (Map.Entry<String, Integer> item : map.entrySet()) {
			customerShopInfo.setFollowerId(null);
			customerShopInfo.setCustomerShopMode(null);
			customerShopInfo.setFollowState(null);
			switch (item.getKey()) {
				case "xkhdfp":
					customerShopInfo.setFollowState(0);
					int num = customerShopRpcService.selectCustomerShopNum(customerShopInfo);
					map.put("xkhdfp", num);
					break;
				case "ygwcl":
					customerShopInfo.setFollowState(1);
					int num1 = customerShopRpcService.selectCustomerShopNum(customerShopInfo);
					map.put("ygwcl", num1);
					break;
				case "xfpdcl":
					customerShopInfo.setFollowState(1);
					customerShopInfo.setFollowerId(customerShop.getFollowerId());
					int num2 = customerShopRpcService.selectCustomerShopNum(customerShopInfo);
					map.put("xfpdcl", num2);
					break;
				case "jrwcl":
					customerShopInfo.setFollowerId(customerShop.getFollowerId());
					customerShopInfo.setCustomerShopMode("04");
					int num3 = customerShopRpcService.selectCustomerShopNum(customerShopInfo);
					map.put("jrwcl", num3);
					break;
				case "gqwcl":
					customerShopInfo.setFollowerId(customerShop.getFollowerId());
					customerShopInfo.setCustomerShopMode("03");
					int num4 = customerShopRpcService.selectCustomerShopNum(customerShopInfo);
					map.put("gqwcl", num4);
					break;
				default:
					break;
			}
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", map);
	}

//	/**
//	 * CRM日常統計分类
//	 *
//	 * @param
//	 * @return
//	 */
//
//
//	@RequestMapping(value = "/countFollowInfo", method = {RequestMethod.POST, RequestMethod.GET})
//	public ResultModel countFollowInfo(CustomerShop customerShop,String searchMode, String searchType, @DateTimeFormat(pattern="yyyy-MM-dd")Date queryDate){
//		if (StringUtils.isEmpty(customerShop.getShopId())) {
//			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
//		}
//		if (StringUtils.isEmpty(searchMode)) {
//			return new ResultModel(ResultCode.RESULT_FAIL, "缺少查询字段");
//		}
//		if (StringUtils.isEmpty(searchType)) {
//			return new ResultModel(ResultCode.RESULT_FAIL, "缺少查询类别");
//		}
//		if(queryDate == null){
//			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空");
//		}
//		switch (searchType){
//			case "followUpInfo":
//				customerShop.setNextFollowDatetime(queryDate);
//				List<Map<String,Object>> resList = customerShopRpcService.selectFollowUpCustomerShopList(customerShop,searchMode);
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", resList);
//			case "orderUpInfo":
//				customerShop.setAppointmentTime(queryDate);
//				List<Map<String,Object>> resList1 = customerShopRpcService.selectFollowUpCustomerShopList(customerShop,searchMode);
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", resList1);
//			case "fixtureInfo":
//				Intention intention = new Intention();
//				intention.setFixtureTime(queryDate);
//				List<Map<String,Object>> resList2 = customerShopRpcService.selectFixtureCustomerShopList(customerShop,intention,searchMode);
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", resList2);
//			default:
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "没有找到数据");
//		}
//
//	}

//	/**
//	 * CRM日常统计列表明细 ??
//	 *
//	 * @param
//	 * @return
//	 */
//
//
//	@RequestMapping(value = "/countFollowInfoList", method = {RequestMethod.POST, RequestMethod.GET})
//	public ResultModel countFollowInfoList(CustomerShop customerShop, String searchType, @DateTimeFormat(pattern="yyyy-MM-dd")Date queryDate){
//		if (StringUtils.isEmpty(customerShop.getShopId())) {
//			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
//		}
//		if (StringUtils.isEmpty(searchType)) {
//			return new ResultModel(ResultCode.RESULT_FAIL, "缺少查询类别");
//		}
//		if(queryDate == null){
//			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空");
//		}
//		switch (searchType){
//			case "followUpInfo":
//				customerShop.setNextFollowDatetime(queryDate);
//				List<CustomerShop> customerShopList = customerShopRpcService.selectFollowUpCustomerShopInfoList(customerShop);
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",setInfoList(customerShopList));
//			case "orderUpInfo":
//				customerShop.setAppointmentTime(queryDate);
//				List<CustomerShop> customerShopList2 = customerShopRpcService.selectFollowUpCustomerShopInfoList(customerShop);
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",setInfoList(customerShopList2));
//			case "fixtureInfo":
//				Intention intention = new Intention();
//				intention.setFixtureTime(queryDate);
//				List<CustomerShop> customerShopList3 = customerShopRpcService.selectFixtureCustomerShopInfoList(customerShop,intention);
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",setInfoList(customerShopList3));
//			default:
//				return new ResultModel(ResultCode.RESULT_SUCCESS, "没有找到数据");
//		}
//
//	}

	/**
	 * CRM日常统计概要
	 *
	 * @param
	 * @return
	 */

	
	@RequestMapping(value = "/countFollowOutline", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel countFollowInfoList(CustomerShop customerShop, @DateTimeFormat(pattern="yyyy-MM-dd")Date queryDate){
		Map<String, Object> res = new HashMap<>();
		if (StringUtils.isEmpty(customerShop.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "店铺ID不能为空");
		}
		if(queryDate == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "查询日期不能为空");
		}
		//跟进中
		Map<String, Object> followUpNum = customerShopRpcService.selectFollowUpNum(customerShop, queryDate);
		//已预约
		Map<String, Object> orderUpNum = customerShopRpcService.selectOrderUpNum(customerShop,queryDate);
		//已成交
		Map<String, Object> fixtureNum = customerShopRpcService.selectFixtureNum(customerShop,queryDate);
		res.put("followUpInfo", followUpNum);
		res.put("orderUpInfo", orderUpNum);
		res.put("fixtureUpInfo", fixtureNum);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	private List<Map<String, Object>> setInfoList(List<CustomerShop> customerShopList) {
		List<Map<String, Object>> resultList = new ArrayList<>();
		Map<String, Object> res = null;
		for(CustomerShop model:customerShopList){
			res = new HashMap<>();
			Customer customer = model.getCustomer();
			if (customer != null){
				res.put("customerName", customer.getCustomerName());
				res.put("customerPhone", customer.getCustomerPhone());
				res.put("levelTxt", model.getLevelTxt());
				res.put("createdTime", customer.getCreateTime());
				res.put("cityName", customer.getCityName());
				res.put("customerShopId", model.getCustomerShopId());
			}
			resultList.add(res);
		}
		return resultList;
	}





}
